package ru.spb.osll.myfit.common;

import com.google.gwt.user.client.ui.Anchor;
import ru.spb.osll.myfit.client.view.ViewConstants;

import java.util.*;

public class PageExecutingResult extends ExecutingResult {
  private static final long serialVersionUID = 8520255971586538783L;

  ArrayList<Table> m_executedTables = new ArrayList<Table>();
  private PageProgressCounts m_counts;


  public PageExecutingResult() {
	  this("-1");
  }
  
  public PageExecutingResult(String id) {
    m_id = id;
    m_counts = new PageProgressCounts();
  }
  
  public PageExecutingResult(PageExecutingResult pageResult) {
    m_id = pageResult.getId();
    m_state = pageResult.getState();
    m_executedTables.addAll(pageResult.getExecutedTables());
    m_counts = pageResult.getCounts();
    m_name = pageResult.getName();
    m_startTime = pageResult.getStartTime();
    m_metadata.putAll(pageResult.getMetadata());
  }
  
  public void setCounts (PageProgressCounts counts) {
    m_counts = counts;
  }
  
  public PageProgressCounts getCounts() {
    return m_counts;    
  }
  
  public void addExecutedTable(Table table){
    m_executedTables.add(table);
  }
  
  public void addAllExecutedTable(List<Table> tables){
    m_executedTables.addAll(tables);
  }

  public List<Table> getExecutedTables(){
    return Collections.unmodifiableList(m_executedTables);
  }
  
  public PageExecutingResult removeAllTables(){
    m_executedTables.clear();
    return this;
  }
  
  public boolean belongsToSuite() {
    return m_metadata.containsKey(TestResultMetadataConstants.SUITE_RESULT);
  }

  public String getSuiteResultId() {
    return m_metadata.get(TestResultMetadataConstants.SUITE_RESULT);
  }

  @Override
  public String getStatusString() {
      int progress = m_counts.getProgressSegments();
      int right = m_counts.getRight();
      int wrong = m_counts.getWrong();
      int ignored = m_counts.getIgnores();
      int exceptions = m_counts.getExceptions();

      return "processed: " + progress + ", right: " + right +
              ", wrong: " + wrong + ", ignored: " + ignored +
              ", exceptions: " + exceptions;
  }

  @Override
  public boolean isWrong() {
    return m_counts.getExceptions() + m_counts.getCounts().getWrong() > 0;
  }

  @Override
  public Anchor getLink() {
    Anchor link = new Anchor(getName(), ViewConstants.TEST_RESULT_ADDRESS + "?id=" + m_id);
    link.setStyleName("pageResultLink");
    return link;
  }

  @Override
  public double getPercentage() {
    return m_counts.getPercentage();
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    PageExecutingResult that = (PageExecutingResult) o;

    if (m_id != null ? !m_id.equals(that.m_id) : that.m_id != null) return false;

    return true;
  }

  @Override
  public int hashCode() {
    return m_id != null ? m_id.hashCode() : 0;
  }
}
